import 'dart:convert';

import 'package:flutter/services.dart';

import 'empUtils/EMPCustomCell.dart';

import 'package:flutter/material.dart';
import 'empUtils/EMPJsonUtil.dart';

class EMPTestCustomGroupCellVC extends StatefulWidget {
  final String title;

  EMPTestCustomGroupCellVC({required this.title});

  @override
  _EMPTestCustomCellVCState createState() => _EMPTestCustomCellVCState();
}

class _EMPTestCustomCellVCState extends State<EMPTestCustomGroupCellVC> {
  List<Group> modules = [];

  @override
  void initState() {
    super.initState();
    fetchData(); // 在初始化时获取数据
  }

  // 模拟网络请求
  Future<void> fetchData() async {
    String jsonString = await rootBundle.loadString('lib/assets/testmodelGroup.json');

    List data = jsonDecode(jsonString);

    // 将解析后的数据转换为 Module 对象列表
    List<Group> newModules = List.from(data).map((json) => Group.fromJson(json)).toList();

    setState(() {
      modules = newModules; // 更新模块列表
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
        itemCount: modules.length,
        itemBuilder: (context, groupIndex) {
          Group group = modules[groupIndex];
          // print('richtitle ${module.params.richtitle}');

          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 分组标题
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 30, vertical: 1),
                child: Container(),
                // child: Text(
                //   group.groupTitle,
                //   style: TextStyle(
                //     fontWeight: FontWeight.bold,
                //     fontSize: 18,
                //   ),
                // ),
              ),
              // 分组列表项
              ListView.builder(
                //设置为 true 时，ListView 的尺寸将会根据其中的内容来动态调整，即列表的高度将会尽可能小，以适应内容的高度。在某些情况下，比如列表嵌套在 Column 或 Row 中，并且需要根据内容来动态调整高度时，可以设置 shrinkWrap: true。
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                itemCount: group.modules.length,
                itemBuilder: (BuildContext context, int itemIndex) {
                  groupModule childItem = group.modules[itemIndex];

                  return EMPCustomCell(
                    //EMPeDefaultCell，EMPAvatarTitleCell，EMPAvatarTitleIconCell,EMPTitleSwitchCell,EMPTitleBtnCell
                    CustomCellType.EMPTitleSwitchCell,
                    '${childItem.name} $itemIndex',
                    () {
                      print('Cell $itemIndex 被点击了');
                    },
                    arrowIcon: 'other/ic_arrow_right',
                    avatarIcon: 'other/icon_avatar',
                    subtitle:
                        'SubtitleSubtitleSubtitletleSubtitletleSubtitleCelltleSubtitleCelltleSubtitle $itemIndex',
                    subtitleTwo:
                        'SubtitleSubtitleSubtitletleSubtitletleSubtitleCelltleSubtitleCelltleSubtitle $itemIndex',
                    rightTitle:
                        'SubtitleSubtitleSubtit哈letleSubtitletleSubtitleCelltleSubtitleCelltleSubtitle $itemIndex',
                    switchValue: childItem.switchValue,
                    onValueChanged: (value) {
                      print('$value');

                      Group group = modules[groupIndex + 1];
                      groupModule module1 = group.modules[itemIndex];
                      module1.name = '这是刷新后的数据';
                      Future.delayed(Duration(seconds: 2), () {
                        setState(() {});
                      });
                    },
                    clickBtn: () {
                      print('点击按钮');
                    },
                    richTitle: childItem.params.richtitle,
                    richColor: '1B6EFD',
                  );
                },
              ),
            ],
          );
        },
      ),
    );
  }
}

class Group {
  final String groupTitle;
  final List<groupModule> modules;

  Group({required this.groupTitle, required this.modules});

  factory Group.fromJson(Map<String, dynamic> json) {
    List<dynamic> modulesList = json['modules'];
    List<groupModule> modules =
        modulesList.map((moduleJson) => groupModule.fromJson(moduleJson)).toList();

    return Group(
      groupTitle: json['groupTitle'],
      modules: modules,
    );
  }
}

class groupModule {
  bool switchValue;
  int age;
  String name;
  String subtitle;
  String subtitleTwo;
  Params params;

  groupModule({
    required this.switchValue,
    required this.age,
    required this.name,
    required this.subtitle,
    required this.subtitleTwo,
    required this.params,
  });

  factory groupModule.fromJson(Map<String, dynamic> json) {
    return groupModule(
      switchValue: json['switch'],
      age: json['age'],
      name: json['name'],
      subtitle: json['subtitle'],
      subtitleTwo: json['subtitleTwo'],
      params: Params.fromJson(json['params']),
    );
  }
}

class Params {
  String richtitle;

  Params({
    required this.richtitle,
  });

  factory Params.fromJson(Map<String, dynamic> json) {
    return Params(
      richtitle: json['richtitle'] ?? '',
    );
  }
}
